package org.budo.support.java.net.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author lmw
 */
public class UrlEncodeUtil {
    public static String encode(String value, String charsetName) {
        try {
            if (null == value) {
                return null;
            }

            return URLEncoder.encode(value, charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decode(String value, String charsetName) {
        if (null == value) {
            return value;
        }

        try {
            return URLDecoder.decode(value, charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decode(String value) {
        return decode(value, "UTF-8");
    }

    public static String encode(String value) {
        return encode(value, "UTF-8");
    }
}